<script>	
	function checkDublicateName()
	{	var k=0;
				for (i=0;i<=400;i=i+6)
					{
					if($("#0"+i).length>0)
					{		$k=0;
							 for (j=0;j<=400;j=j+6)
							{
							if($("#0"+j).length>0 && $("#0"+i).val()==$("#0"+j).val())
							{
							 $k++;
							 
							}
							}
						if($k>1)
					{
					alert("Dublicate stock Entry. please remove new and add stock in existing one !");
					
					}
				 	 
					}
					}
			
	}

	function callAutoAsignValue(idname)
	{
			
			 var name1 = parseInt(idname,10);
			 var quantity1 = name1+1;
			 var brate1 =  quantity1+1;
			 var srate1 =  brate1+1;
			 var avail1 = srate1+1;
			 var total1 = avail1+1;
			
			 if(parseInt(idname)>0)
			 {
			 quantity1="00"+quantity1;
			 brate1="000"+brate1;
			 srate1="0000"+srate1;
			 avail1="00000"+avail1;
			 total1="000000"+total1;
			 
			 }
			 else
			 {
			  quantity1="00";
			  brate1="000";
			  srate1="0000";
			  avail1="00000";
			  total1="000000";
			  
			 }
			 
				
		$.post('sales_check_details.php', {stock_name: $("#"+idname).val() },
				function(data){
				
								// if(data=='no') //if username not avaiable
		 						// {
								//  $("#category").focus();
								// }
															
								$("#"+brate1).val(data.brate);
								$("#"+srate1).val(data.rate);
								$("#"+avail1).val(data.availstock);
								$("#quantity").focus();
							}, 'json');
							
							
						checkDublicateName();	
							
	}
	function callQKeyUp(Qidname)
	{		
			 var quantity = parseInt(Qidname,10);
			 var brate =  quantity+1;
			 var srate =  brate+1;
			 var avail = srate+1;
			 var total = avail+1;
			 var rowcount = parseInt((total+1)/5);
			 if(rowcount==0)
			 rowcount=1;
			
			 if(parseInt(Qidname)>0)
			 {
			 quantity="00"+quantity;
			 brate="000"+brate;
			 srate="0000"+srate;
			 avail="00000"+avail;
			 total="000000"+total
			 }
			 else
			 {
			  quantity="00";
			  brate="000";
			  srate="0000";
			  avail="00000";
			  total="000000";
			  
			  
			 }
			var result= parseFloat($("#"+quantity).val()) * parseFloat( $("#"+brate).val() );
			result=result.toFixed(2);
			$("#"+total).val(result);
			updateSubtotal();
			
	}
	function balanceCalc()
	{		if(parseFloat($("#payment").val()) > parseFloat($("#subtotal").val()))
			$("#payment").val(parseFloat($("#subtotal").val()));
			
			var result= parseFloat($("#subtotal").val()) - parseFloat( $("#payment").val() );
			result=result.toFixed(2);
			$("#balance").val(result);
	}
	function updateSubtotal()
	{					
					var temp=0;
					for (i=5;i<=400;i=i+6)
					{
					if($("#000000"+i).length>0)
					{
					 temp=parseFloat(temp)+parseFloat($("#000000"+i).val());
				 	 
					}
					}
				
			
			var subtotal=parseFloat(temp);
			
			if($("#000000").length>0)
			{
			var firstrowvalue=$("#000000").val();
			
			subtotal=parseFloat(subtotal)+parseFloat(firstrowvalue);
			}
			subtotal=subtotal.toFixed(2);
			$("#subtotal").val(subtotal);
			
			
	}
	
	function callRKeyUp(Ridname)
	{
			 var brate = parseInt(Ridname,10);
			 var quantity =  brate-1;
			 var srate =  brate+1;
			 var avail = srate+1;
			 var total = avail+1;
			 
			 callQKeyUp(brate-1)
	}
	</script>
<table class="bordered" width="770">
                    <thead>
                        <tr>
                            <th>Edit Stock information:<br /><span class="red">* require feild</span></th>
                        </tr>
                        <tr>
                        	<?php 
							  if(@$errors!=""){
							  	echo " <td><span class='red'>". @$errors."</span></td>";
							  }else{
								  if(isset($_REQUEST['msgStatus']))
									echo " <td><span class='red'>". $_REQUEST['msgStatus'].@$_REQUEST['one']."</span></td>";
							  }
							?>
                           
                        </tr>
                    </thead>
</table>
<?php 
	if(isset($_REQUEST['control'])){
		include("add_stock_verify.php");
	}
?>
<?php
	//select record to show
	@$stock_id=$_REQUEST['stock_id'];
	if(isset($_REQUEST['stock_id'])){
		$sql ="SELECT * FROM tbl_stocks WHERE stock_id='$stock_id'";	
		$result=query($sql);
		$row=fetch_assoc($result);
		$stockNumber=$row['stock_id'];
		$billNumber=$row['billnumber'];
		$supplier=$row['sup_name'];
		$pd_date=$row['pd_date'];
		$id=$row['id'];
	}
?>
<form class="form" action="index.php?page=add_stock&action=add_stock_edit&control=update" method="post" name="edit" onSubmit="updateSubtotal()" >
	<table width="800"  id="dynamictable" style="margin:20px 0px 0px 0px;">
                  		<tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td width="1">&nbsp;</td>
                       </tr> 
                        <tr>
                        	<td width="67"><span class="big">Stock ID </span>  
                          </td>
                          <td width="105"><input name="stocknumber" type="text" id="stocknumber" readonly value="<?php echo @$stockNumber; ?>" style="width:50px;"></td>
                          <td width="55"><span class="big">Bill No </span></td>
                          <td width="115"><input type="text" name="billnumber" style="width:60px;" readonly value="<?php echo @$billNumber;?>" id="billnumber" class="validate[required,length[0,100]] text-input"></td>
                          <td width="36"><span class="big">Date</span></td>
                          <td width="138"><input type="text" id="date" name="date" class="date_input" value="<?php echo @$pd_date;?>" style="width:70px;"></td>
                          <td width="61"><span class="big">Supplier</span></td>
                          <td width="182">
                          		<select name="supplier" class="input req-min" minlength="1" style="width:150px; height:25px;">
                                     <option value=""><--Please Supplier--></option>
                                        <?php
                                            $sql="SELECT * FROM tbl_suppliers ORDER BY id DESC";
                                            $result=query($sql);
                                            while($row=fetch_object($result))
                                            {
												if(@$supplier == $row->sup_name)
													$selected = " selected='selected'";
												else	
													$selected = "";
                                        ?>
                                      <option value="<?php echo $row->sup_name;?>"<?php echo $selected;?>><?php echo $row->sup_name;?></option>
                                        <?php 
                                            }
                                        ?>
                          		</select>
                          </td>
                          <td></td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                       </tr> 
               </table>

                  <br>
					<?php 
					$max= maxOf("count", "tbl_stocks", "stock_id='$stock_id'");
					$j=0;
					for($i=1; $i<=$max; $i++)
					{
					
					?>
					<table width="800" border="0" cellspacing="0" cellpadding="0"  style="margin-left:0px;">
  						<tr>
                      <td ><span class="smal">Name</span></td>
                      <td >
					  <input name="sysid[]" type="hidden" value="<?php echo $id;?>">
                      
					  <?php 
					  $sql ="SELECT * FROM tbl_stocks WHERE stock_id='$stock_id' and count=$i ";	
					  $result=query($sql);
					  $row=fetch_assoc($result);
					  $pdName=$row['pd_name'];
					  
					  ?>
                      <select name="name[]" id="0<?php echo 0+$j;?>" onBlur="callAutoAsignValue(this.id)" autocomplete="off" class="text_smal">
                                 <option value=""> -- Select Product -- </option>
										<?php
											$sql = "SELECT * FROM tbl_products ORDER BY pd_id DESC";
											$result = mysql_query($sql);
											while($row_pd = mysql_fetch_array($result))
											{
												if(@$pdName == $row_pd['pd_name'])
													$selected = " selected='selected'";
												else	
													$selected = "";
                                		?>
                                        <option value="<?php echo $row_pd['pd_name'];?>"<?php echo $selected;?>> <?php echo $row_pd['pd_name'];?></option>
										<?php
                                            }
                                            
                                        ?>
                       </select>
                      
                      </td>
                      <td><span class="smal">Qty</span></td>
                      <td><input name="quantity[]" type="text" id="00<?php echo 1+$j;?>"  class="input req-min" minlength="1"  style="width:50px;" onKeyUp="callQKeyUp(this.id)" value="<?php echo $row ['qty'];?>"></td>
                      <td><span class="smal">Buy Price</span></td>
                      <td><input name="brate[]" type="text" id="000<?php echo 2+$j;?>" class="input req-min" minlength="1"  style="width:50px;" onKeyUp="callRKeyUp(this.id)"  value="<?php echo $row['buying_price'];?>" ></td>
					  <td><span class="smal">Sale Price</span> </td>
					  <td><input name="srate[]" type="text" id="0000<?php echo 3+$j;?>" class="input req-min" minlength="1"  style="width:50px;" value="<?php echo $row['selling_price'];?>"   ></td>
					  <td><span class="smal">Avail Qty</span></td>
						<td><input name="avail[]" class="input req-min" minlength="1" type="text" id="00000<?php echo 4+$j;?>" readonly value="<?php echo $row['closing_stock'];?>" style="width:50px;" ></td>
                      <td><span class="smal">Total:</span></td>
                      <td><input name="total[]" class="input req-min" minlength="1" type="text" id="000000<?php echo 5+$j;?>" readonly value="<?php echo $row['total'];?>" style="width:100px;text-align:right;" >  </td>
                      
                    </tr>
					  <tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						
					  </tr>
					  </table>
					  
					  <?php
					  $j=$j+6;
					  }
					  ?>
					  <table width="800" border="0" align="center" cellpadding="0" cellspacing="0"  id="duplicate" style="margin-left:20px;">
					  <tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td width="233">&nbsp;</td>
					  </tr>
					  <tr>
						<td><span class="big">Payment:</span></td>
						<td><input type="text" class="input req-min" minlength="1" name="payment" style="width:100px;" id="payment" onKeyUp="balanceCalc()"  value="<?php echo $row['paid']; ?>" ></td>
						<td><span class="big">Balance:</span></td>
						<td><input name="balance" type="text" id="balance" style="width:100px; " value="<?php echo $row['balance']; ?>" readonly ></td>
                        <td width="75"><span class="big">Sub Total </span></td>
						<td width="233"><input name="subtotal" id="subtotal" type="text" readonly value="<?php echo $row['amount']; ?>" style="width:100px; text-align:right; color:#333333; font-weight:bold; font-size:16px;"><img src="images/refresh.png" alt="Refresh" align="absmiddle" onClick="updateSubtotal()" style="cursor:pointer;"></td>
					  </tr>
					  <tr>
						<td></td>
						<td></td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td><div align="center"></div></td>
						<td>&nbsp;</td>
						
					  </tr>
					  <tr>
						<td width="74"><span class="big">Mode:</span></td>
						<td width="167"><select name="mode">
						<option value="cheque" <?php if($row['mode']=="cheque") echo "selected"; ?> >Cheque</option>
						<option value="cash" <?php if($row['mode']=="cash") echo "selected"; ?> >Cash</option>
						<option value="others" <?php if($row['mode']=="others") echo "selected"; ?> >others</option>
						
						</select></td>
						<td width="74"><span class="big">Due Date </span></td>
						<td width="177"><input type="text" id="lastupdate" name="lastupdate" class="date_input" value="<?php 
						$phpdate = strtotime($row['pd_last_update']);
				
						$phpdate = date("d-m-Y",$phpdate);
						echo $phpdate;
?>" style="width:70px;"></td>
						<td>
                        	
                        </td>
						<td >
                        	<input class="btnSubmit" type="submit" name="Submit" value="Update" onClick="updateSubtotal()" >
                        	<input class="btnSubmit" type="button" name="Reset" value="Cancel" onclick="redirect('index.php?page=add_stock&action=add_stock')">
                        </td>
					  </tr>
					</table>
</form>